import json

import requests
from pymongo import MongoClient


database_name = "shiyu"

# 使用用户名和密码连接MongoDB
mongo_client = MongoClient(host="localhost", port=27017)



headers = {
    "Accept": "application/json, text/plain, */*",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Connection": "keep-alive",
    "Content-Type": "application/x-www-form-urlencoded",
    "Origin": "https://www.zongheng.com",
    "Referer": "https://www.zongheng.com/books?worksTypes=0&subWorksTypes=0&totalWold=0&serialStatus=1&vip=9",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-origin",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36",
    "sec-ch-ua": "\"Google Chrome\";v=\"119\", \"Chromium\";v=\"119\", \"Not?A_Brand\";v=\"24\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\""
}
cookies = {
    "ZHID": "F9E7F3260F818623072E7AA25D62BFA1",
    "zh_visitTime": "1699668164918",
    "Hm_lvt_c202865d524849216eea846069349eb9": "1699668165",
    "b-user-id": "c634da0f-7a18-4c47-c99f-9cb3244a116a",
    "zhffr": "www.google.com",
    "sensorsdata2015jssdkcross": "%7B%22distinct_id%22%3A%2218bbc1e0121581-0d7951affc923c-26031051-1327104-18bbc1e0122352%22%2C%22%24device_id%22%3A%2218bbc1e0121581-0d7951affc923c-26031051-1327104-18bbc1e0122352%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E8%87%AA%E7%84%B6%E6%90%9C%E7%B4%A2%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fwww.google.com%2F%22%2C%22%24latest_referrer_host%22%3A%22www.google.com%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%7D%7D",
    "acw_tc": "0bc5052c17004496965351025eb01191824675a29504d09630a060d06d568f"
}
url = "https://www.zongheng.com/api2/catefine/storeSearch"
for page in range(0, 150 + 1):

    data = {
        "worksTypes": "0",
        "bookType": "0",
        "subWorksTypes": "0",
        "totalWord": "0",
        "serialStatus": "1",
        "vip": "9",
        "totalWold": "0",
        "pageNum": page,
        "pageSize": "20",
        "categoryId": "0",
        "cateFineId": "0",
        "order": "weekOrder"
    }
    response = requests.post(url, headers=headers, cookies=cookies, data=data)

    result = json.loads(response.text)
    result_ul = (result["result"]["bookList"])
    for resut_li in result_ul:
        bookId = resut_li["bookId"]
        name = resut_li["name"]
        picUrl = resut_li["picUrl"]
        description = resut_li["description"]
        authorId = resut_li["authorId"]
        authorName = resut_li["authorName"]
        serialStatus = resut_li["serialStatus"]
        cateFineId = resut_li["cateFineId"]
        cateFineName = resut_li["cateFineName"]
        categoryId = resut_li["categoryId"]
        categoryName = resut_li["categoryName"]
        totalWords = resut_li["totalWords"]
        theNewChapter = resut_li["theNewChapter"]
        theNewChapterId = resut_li["theNewChapterId"]
        orderColumnInfo = resut_li["orderColumnInfo"]

        print(page, bookId, name, picUrl, description, authorId, authorName, serialStatus, cateFineId, cateFineName,
              categoryId, categoryName, totalWords)
        resut_data = {
            "bookId": bookId,
            "name": name,
            "picUrl": picUrl,
            "description": description,
            "authorId": authorId,
            "authorName": authorName,
            "serialStatus": serialStatus,
            "cateFineId": cateFineId,
            "cateFineName": cateFineName,
            "categoryId": categoryId,
            "categoryName": categoryName,
            "totalWords": totalWords,
            "theNewChapter": theNewChapter,
        }
        # # 将数据插入到 MongoDB
        # collection.insert_one(resut_data)
        #
        # # 打印数据（可选）
        # print(resut_data)
        result2 = mongo_client.Collections.concat.insert_one(resut_data)
        print(result2)
